<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AdminPermissionRequest;
use App\Models\AdminPermission;
use Auth;

class AdminPermissionsController extends Controller
{
    public function index()
    {
        return AdminPermission::orderBy('sort')->orderBy('id')->get();
    }

    public function store(AdminPermissionRequest $request)
    {
        $permission = AdminPermission::create($request->only('pid', 'title', 'name', 'sort', 'icon', 'type', 'status'));
        return $permission;
    }

    public function edit(AdminPermission $adminPermission)
    {
        return $adminPermission;
    }

    public function update(AdminPermissionRequest $request, AdminPermission $adminPermission)
    {
        $adminPermission->update($request->only('pid', 'title', 'name', 'sort', 'icon', 'type', 'status'));
        return $adminPermission;
    }

    public function destroy(AdminPermission $adminPermission)
    {
        $adminPermission->delete();

        // 关联用户、角色
        $adminPermission->users()->detach();
        $adminPermission->roles()->detach();
    }




}